﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Devtm.Parsing
{

    public enum Token
    {
        OpenBrace,
        CloseBrace,
        Text,
        DoublePoint,
        Eof
    }


    [System.Diagnostics.DebuggerStepThrough]
    public class Lexer
    {

        public string Expression { get; private set; }
        const string pattern = @"{|[^\:\}\{\n,]+|[:,]|}";
        Regex reg = new Regex(pattern, RegexOptions.Compiled);

        public Lexer(string expresion)
        {
            // TODO: Complete member initialization
            this.Expression = expresion;
        }

        public Token Next()
        {
            if (ResultMatch == null)
                ResultMatch = reg.Match(Expression);

            else
                ResultMatch = ResultMatch.NextMatch();

            if (ResultMatch.Success)
            {
                Value = ResultMatch.Value;

                switch (ResultMatch.Value)
                {

                    case "{":
                        return Token.OpenBrace;

                    case "}":
                        return Token.CloseBrace;

                    case ":":
                        return Token.DoublePoint;

                    default:
                        return Token.Text;

                }

            }

            return Token.Eof;

        }

        public string Value { get; private set; }

        public Match ResultMatch { get; private set; }

    }

}
